
import 'package:flutter/material.dart';

class EasyInput extends StatefulWidget {
  // 传入参数
  const EasyInput({
    Key? key,
    this.title = '输入',
    this.hintText = '请输入：',
    this.maxLines = 1,
    this.keyboardType = 'text',
    this.inputAction = 'none',
    this.value = '',
    this.autofocus = true,
    this.obscureText = false,
    required this.values,
  }) : super(key: key,);
  final String title;
  final String hintText;
  final int maxLines;
  final String keyboardType;
  final String inputAction;
  final String value;
  final ValueChanged<String> values;
  final bool autofocus;
  final bool obscureText;

  @override
  State<StatefulWidget> createState() => EasyInputState();

}

class EasyInputState extends State<EasyInput> {

  TextEditingController value_ = TextEditingController();
  Map typ = {
    'text':TextInputType.text,
    'number':TextInputType.number,
    'multiline':TextInputType.multiline,
    'phone':TextInputType.phone,
    'datetime':TextInputType.datetime,
    'emailAddress':TextInputType.emailAddress,
    'url':TextInputType.url,
    'visiblePassword':TextInputType.visiblePassword,
    'name':TextInputType.name,
    'streetAddress':TextInputType.streetAddress
  };

  Map inputAction = {
    'none':TextInputAction.none,
    'unspecified':TextInputAction.unspecified,/// 操作系统自己选择最优
    'done':TextInputAction.done,///  完成
    'go':TextInputAction.go,///  前往
    'search':TextInputAction.search,///  搜索
    'send':TextInputAction.send,///  发送
    'next':TextInputAction.next,///  下一项
    'previous':TextInputAction.previous,///  上一个(ios不支持)
    'continueAction':TextInputAction.continueAction,///  继续(仅ios支持)
    'join':TextInputAction.join,///  加入(仅ios支持)
    'route':TextInputAction.route,///  路线(仅ios支持)
    'emergencyCall':TextInputAction.emergencyCall,///  紧急呼叫(仅ios支持)
    'newline':TextInputAction.newline,///  安卓(换行回车)ios(返回)
  };
  @override
  Widget build(BuildContext context) {
    if ( widget.value != ''){
      value_.text = widget.value;
    }

    return Container(
      padding: const EdgeInsets.fromLTRB(8, 8, 8, 8),
      child: TextField(
        controller: value_,
        maxLines: widget.maxLines,
        style: const TextStyle(color: Colors.black,fontSize: 18),
        keyboardType: typ.containsKey(widget.keyboardType)?typ[widget.keyboardType]:TextInputType.text,
        textInputAction: inputAction.containsKey(widget.inputAction)?inputAction[widget.inputAction]:TextInputAction.next,
        decoration: InputDecoration(
          suffixIcon: IconButton(
              onPressed: () {value_.clear();widget.values('');},
              icon: const Icon(Icons.cancel, color: Colors.black38,size: 18,)
          ),
          contentPadding: const EdgeInsets.fromLTRB(16, 10, 10, 10),
          hintText: widget.hintText,
          labelText: widget.title,
          border: const OutlineInputBorder(),
        ),
        autofocus: widget.autofocus,
        obscureText: widget.obscureText,
        onChanged: (val){widget.values(val);},
      )
    );
  }


}